import 'package:bot_toast/bot_toast.dart';
import 'package:core/provider/global_key_provider.dart';
import 'package:core/route/core_navigator_observer.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_picker/picker_localizations_delegate.dart';

import '../../extension/chinese_cupertino_localizations.dart';
import '../../helper/global_helper.dart';
import '../../route/core_router.dart';

class BaseMaterialApp extends StatelessWidget {
  final String title;
  final ThemeData? theme;
  final List<NavigatorObserver>? navigatorObservers;
  final List<LocalizationsDelegate<dynamic>>? localizationsDelegates;
  final bool debugShowCheckedModeBanner;
  final List<Locale> supportedLocales;
  final String? initialRoute;
  final Widget? home;
  final Map<String, WidgetBuilder> routeList;

  const BaseMaterialApp({
    super.key,
    this.title = '',
    this.theme,
    this.navigatorObservers,
    this.localizationsDelegates,
    this.debugShowCheckedModeBanner = true,
    this.supportedLocales = const [Locale('en', 'US'), Locale('zh', 'CN')],
    this.initialRoute,
    this.home,
    required this.routeList,
  });

  @override
  Widget build(BuildContext context) {
    final botToastBuilder = BotToastInit();
    var navigatorObserversNew = navigatorObservers ?? [];
    navigatorObserversNew.addAll([
      BotToastNavigatorObserver(),
      CoreNavigatorObserver(),
    ]);
    var localizationsDelegatesNew = localizationsDelegates ?? [];
    localizationsDelegatesNew.addAll([
      PickerLocalizationsDelegate.delegate,
      DefaultCupertinoLocalizations.delegate,
      ChineseCupertinoLocalizations.delegate,
      GlobalWidgetsLocalizations.delegate,
      GlobalMaterialLocalizations.delegate,
    ]);
    routes.addAll(routeList);
    return MaterialApp(
      navigatorKey: GlobalKeyProvider.navigatorKey,
      title: title,
      theme: theme ??
          ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.blueAccent),
            useMaterial3: true,
          ),
      builder: (context, child) {
        child = Scaffold(
          resizeToAvoidBottomInset: false,
          body: GestureDetector(
            onTap: () => GlobalHelper.hideKeyboard(),
            child: child,
          ),
        );
        child = botToastBuilder(context, child);
        return child;
      },
      navigatorObservers: navigatorObserversNew,
      localizationsDelegates: localizationsDelegatesNew,
      debugShowCheckedModeBanner: debugShowCheckedModeBanner,
      supportedLocales: supportedLocales,
      localeResolutionCallback: (Locale? locale, Iterable<Locale> supportedLocales) {
        return locale;
      },
      onGenerateRoute: onGenerateRoute,
      initialRoute: initialRoute,
      home: home,
    );
  }
}
